package dashboard;

import java.io.IOException;
import javax.servlet.http.*;


import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@SuppressWarnings("serial")
public class MilestoneServlet extends HttpServlet
{
	private MilestoneController MC = new MilestoneController();

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String action= req.getParameter("action");
		if(action.equals("view")){
			User currentUser = (User) req.getSession().getAttribute("user");
			Date now = new Date();
			List<Milestone> milestones = currentUser.getMilestones();
			List<Milestone> result = new ArrayList<Milestone>();			


			int j=milestones.size();
			int k = 0;
			while(k<5 && k<j){
				if(milestones!=null){
					Milestone max = null;
					boolean started =false;
					int i = 0;
					while(started==false && i<j){
						max= milestones.get(i);
						i++;
						if(max.getEnd().after(now) && !result.contains(max)){
							started=true;	
						}					
					}
					if(max!=null){		
						for(Milestone mi : milestones){				
							if(mi.getEnd().before(max.getEnd()) && mi.getEnd().after(now) && !result.contains(mi)){
								max = mi;
							}
						}

					}
					k++;
					if(max!=null){
						result.add(max);
					}
				}
			}




			HttpSession session=req.getSession();
			session.setAttribute("5milestones",result);
			resp.sendRedirect("milestone.jsp?action=viewStart");
		}
	}	

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException 
	{
		List<String> errors = new ArrayList<String>();
		String redirectTo = "dashboard.jsp?I=miladded";
		User currentUser = (User) req.getSession().getAttribute("user");
		String action = (String) req.getParameter("action");

		// toevoegen van vakken aan de milestone

		if(action.equals("courseSelection"))
		{
			List<Long> courses = new ArrayList<Long>();
			if(req.getParameter("course").equals("All courses")){
				courses = currentUser.getCourses();
			}
			else 
				courses.add(Long.parseLong(req.getParameter("course")));

			errors = MC.setCourses(courses);
			if(!errors.isEmpty())
				redirectTo = "milestone.jsp?action=add";
			else
				redirectTo = "milestone.jsp?action=type";
		}
		else if(action.equals("typeSelection")){

			errors = MC.setTypes( (String[]) req.getParameterValues("type"));

			if(!errors.isEmpty())
				redirectTo = "milestone.jsp?action=type";
			else
				redirectTo = "milestone.jsp?action=date";

		}
		else if(action.equals("dateSelection"))
		{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date stop = new Date();
			Date start = new Date();
			try{
				stop = df.parse(req.getParameter("stop"));
				start = df.parse(req.getParameter("start"));
			} catch (ParseException e) {
				e.printStackTrace();
			} finally {

			}
			errors = MC.setDates(start, stop);

			if(!errors.isEmpty())
				redirectTo = "milestone.jsp?action=date";
			else
				redirectTo = "milestone.jsp?action=goal";
		}
		else if(action.equals("goalSelection"))
		{
			double goal=-1;
			try{
				goal = Double.parseDouble( req.getParameter("goal"));			
			}
			catch (NumberFormatException fout) {
				errors.add("This isn't a valid number");
			}
			String act = req.getParameter("act");
			MC.setAction(act);
			errors = MC.setGoal(goal);

			if(!errors.isEmpty())
				redirectTo = "milestone.jsp?action=goal";
			else
				redirectTo = "milestone.jsp?action=confirmation";
		}
		else if(action.equals("clear"))
		{
			MC.clear();
			redirectTo="milestone.jsp?action=add";
		}
		else if(action.equals("create"))
		{
			MC.addMilestone(currentUser);
			req.getSession().setAttribute("currentUser",currentUser);
			
		}


		req.getSession().setAttribute("errors", errors);
		req.getSession().setAttribute("cMilestone", MC.getMilestone());
		resp.sendRedirect(redirectTo);
	}

}